﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.BandedGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using INSTA.StockApplication.BLL;

namespace INSTA.StockApplication.UI.Windows
{
    public partial class RibbonForm1 : System.Windows.Forms.Form
    {

        public RibbonForm1()
        {
            InitializeComponent();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {

        }



        private void barButtonItem3_ItemClick(object sender, ItemClickEventArgs e)
        {
            gridControl1.DataSource = null;
            gridControl1.MainView = gridView2;
            gridControl1.DataSource = Products.GetListOfProductsFromDatabase();
        }

        private void ribbon_Click(object sender, EventArgs e)
        {

        }

        private void barButtonItem7_ItemClick(object sender, ItemClickEventArgs e)
        {
            string messageBoxText = "Etes-vous sûr de vouloir vous déconnecter?";
            var result = XtraMessageBox.Show(messageBoxText, "Suppression", MessageBoxButtons.YesNoCancel,
                                             MessageBoxIcon.Warning);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void barButtonItem1_ItemClick_1(object sender, ItemClickEventArgs e)
        {

        }

        private void barDockingMenuItem1_ListItemClick(object sender, ListItemClickEventArgs e)
        {

        }

        private void barButtonItem9_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonItem11_ItemClick(object sender, ItemClickEventArgs e)
        {

        }

        private void barButtonItem10_ItemClick(object sender, ItemClickEventArgs e)
        {
            var c = new Customer();
            gridControl1.DataSource = null;
            gridControl1.MainView = gridView1;
            gridControl1.DataSource = c.GetListOfUsersFromDatabase();

        }

        private void barButtonItem4_ItemClick(object sender, ItemClickEventArgs e)
        {
            var c = new Orders();
            gridControl1.DataSource = null;
            gridControl1.MainView = gridView3;
            gridControl1.DataSource = c.GetOrdersFromDatabase();



        }

        private void gridView3_MouseEnter(object sender, EventArgs e)
        {

        }

        private void gridView3_GroupRowCollapsed(object sender, DevExpress.XtraGrid.Views.Base.RowEventArgs e)
        {

        }

        private void gridView3_GroupRowCollapsing(object sender, DevExpress.XtraGrid.Views.Base.RowAllowEventArgs e)
        {

        }

        private void gridView3_RowClick(object sender, RowClickEventArgs e)
        {
        }

        private void gridView3_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_RowCellClick(object sender, RowCellClickEventArgs e)
        {
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            if (gridControl1.MainView == gridView1)
            {
                var t = new XtraForm1();
                t.Show();
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {

        }

        private int DeleteRow(GridView grid, string field)
        {

            int[] i = grid.GetSelectedRows();
            return Convert.ToInt32(grid.GetRowCellValue(i[0], field).ToString());

        }

        private void simpleButton1_Click_1(object sender, EventArgs e)
        {
            switch (gridControl1.MainView.Name)
            {
                case "gridView1":
                    if (gridView1.SelectedRowsCount > 0)
                    {
                        var c = new Customer();
                        c.DeleteCustomerFromDatabase(DeleteRow(gridView1, "ID"));
                    }
                    break;
                case "gridView3":
                    if (gridView3.SelectedRowsCount > 0)
                    {
                        var c = new Orders();
                        c.DeleteOrderFromDatabase(DeleteRow(gridView3, "ID_COMMANDE"));
                    }
                    break;
            }
            gridControl1.RefreshDataSource();
        }

        private void barButtonItem5_ItemClick(object sender, ItemClickEventArgs e)
        {
            gridControl1.DataSource = null;
            gridControl1.MainView = gridView4;
            gridControl1.DataSource = Provider.GetListFournisseurFromDatabase();
            
        }

        private void gridView4_MasterRowGetChildList(object sender, MasterRowGetChildListEventArgs e)
        {
            e.ChildList = Products.GetListOfProductsFromDatabase();
        }

        private void RibbonForm1_Load(object sender, EventArgs e)
        {

        }

        private void simpleButton2_Click_1(object sender, EventArgs e)
        {
            switch(gridControl1.FocusedView.Name)
            {
                case "gridView3" :
                    var form = new XtraForm1();
                    form.Show();
                    break;
                case "gridView2" :
                    var form3 = new AddProduct();
                    form3.Show();
                    break;
                case "gridView1" :
                    var form2 = new AddUsers();
                    form2.Show();
                    break;
            }
        }

        private void gridView1_Click(object sender, EventArgs e)
        {
            int[] rowsSelected = gridView1.GetSelectedRows();
            int id = Convert.ToInt32(gridView1.GetRowCellValue(rowsSelected[0],"ID"));
            if(id > 0)
            {
                
                string nom = gridView1.GetRowCellValue(rowsSelected[0], "NOM").ToString();
                string prenom = gridView1.GetRowCellValue(rowsSelected[0], "PRENOM").ToString();
                string mail = gridView1.GetRowCellValue(rowsSelected[0], "MAIL").ToString();
                string adresse = gridView1.GetRowCellValue(rowsSelected[0], "ADRESSE").ToString();
                string cp = gridView1.GetRowCellValue(rowsSelected[0], "CP").ToString();
                string ville = gridView1.GetRowCellValue(rowsSelected[0], "VILLE").ToString();
                string pays = gridView1.GetRowCellValue(rowsSelected[0], "PAYS").ToString();
                string telephone = gridView1.GetRowCellValue(rowsSelected[0], "TELEPHONE").ToString();

                var form = new AddUsers(id, nom, prenom, mail, adresse, cp, ville, pays, telephone);
                form.Show();

            }
        }

        private void barButtonItem6_ItemClick(object sender, ItemClickEventArgs e){
            var c = new Inventory();
            gridControl1.DataSource = null;
            gridControl1.MainView = gridView5;
            gridControl1.DataSource = c.GetStockFromDatabase();
        }
    }
}